<include file="Public:header"/>
<div class="container" id="apiTool">
	<div class="row">
		<div class="col-lg-5" style="width:32%;">
			<div class="parameters form-horizontal">
				<div class="form-group">
					<label for="" class="col-sm-4 control-label">返回格式：</label>
					<div class="col-sm-8">
						<span class="form-control">JSON</span>
					</div>
				</div>
				<div class="form-group">
					<label for="" class="col-sm-4 control-label">所有项目：</label>
					<div class="col-sm-8">
						<select name="project" class="form-control" id="project">
							<option>--请选择--</option>
							<volist name="project_list" id="vo">
								<option value="{$vo.p_id}" data-href="{$vo.p_api_head}">{$vo.p_name}</option>
							</volist>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label for="" class="col-sm-4 control-label">当前模块：</label>
					<div class="col-sm-8">
						<select name="module" class="form-control" id="mo">
							<option>--请选择--</option>
						</select>
					</div>
				</div>
				<div class="form-group">
					<label for="" class="col-sm-4 control-label">当前方法：</label>
					<div class="col-sm-8">
						<select name="function" class="form-control" id="fun">
							<option>--请选择--</option>
						</select>
					</div>
				</div>
				<form action="" post="" name="subApiTool" class="subApiTool">
					<input type="hidden" value="" id="p_api_head" />
					<input type="hidden" value="" id="mo_e_name" />
					<input type="hidden" value="" id="f_name" />
					<input type="hidden" value="" id="f_url" />
					<input type="hidden" value="" id="f_type" />
					<div id="addParameters">
					</div>
					<div class="form-group">
						<div class="col-sm-offset-3 col-sm-8" style="margin-left: 33%;">
							<button type="button" class="btn btn-primary" id="subTool">提交测试</button>
						</div>
					</div>
				</form>
			</div>
		</div>
		<div class="col-lg-7" style="width:68%;">
			<div class="return ">
				<div class="return1">
					<p>API请求参数： </p>
					<textarea class="form-control" rows="3" cols="90" id="apiPa"></textarea>
				</div>
                <div class="return1">
                    <p>整理后的返回结果： </p>
                    <textarea class="form-control" rows="18" cols="90" id="sdkBox"></textarea>
                </div>
				<div class="return1">
					<p>源生返回结果： </p>
					<textarea class="form-control" rows="6" cols="90" id="apiBox"></textarea>
				</div>
			</div>
		</div>
	</div>
</div>
<script type="text/javascript">
	$('#project').change(function () {
	    var pid = $(this).val();
	    $.ajax({
	        url : "{:U('ApiTool/ajaxModule')}",
	        type :'POST',
	        dataType:"json",
	        data:{pid:pid},
	        success:function(data){
	            if(data){
	                $('#mo').empty();
	                var data1 = data.molist;
	                var phead = data.phead;
	                $('#p_api_head').val(phead);
	                var i = 0;
	                var option="";
	                option+='<option>--请选择--</option>';
	                for(i=0;i<data1.length;i++){
	                    option+="<option value='"+data1[i].mo_id+"'>"+data1[i].mo_name+"</option>";
	                }
	                $('#mo').html(option);
	            } 
	        }
	    });
	});
	$('#mo').change(function () {
	    var moid = $(this).val();
	    $.ajax({
	        url : "{:U('ApiTool/ajaxFun')}",
	        type :'POST',
	        dataType:"json",
	        data:{moid:moid},
	        success:function(data){
	            if(data){
	                $('#fun').empty();
	                var data1 = data.flist;
	                var moename = data.moename;
	                $('#mo_e_name').val(moename);
	                var i = 0;
	                var option="";
	                option+='<option>--请选择--</option>';
	                for(i=0;i<data1.length;i++){
	                    option+="<option value='"+data1[i].f_id+"'>"+data1[i].f_name+"</option>";
	                }
	                $('#fun').html(option);
	            }  
	        }
	    });
	});
	$('#fun').change(function () {
	    var f_id = $(this).val();
	    $.ajax({
	        url : "{:U('ApiTool/ajaxParameters')}",
	        type :'POST',
	        dataType:"json",
	        data:{f_id:f_id},
	        success:function(data){
	            if(data){
	                $('#ajaxParameters').empty();
	                $('#f_name').val(data.fname);
	                $('#f_url').val(data.furl);
	                $('#f_type').val(data.ftype);
	                data1 = data.pa;
	                createPa(data1,data.ftype);
	            }
	        }
	    });
	});

	$('#subTool').click(function(){
		$(this).text('数据拉取中...');
		$(this).css({'background-color':'#84B4DD','border-color':'#84B4DD'});
		var p_api_head = $('#p_api_head').val();
		var mo_e_name = $('#mo_e_name').val();
		var f_name  =$('#f_name').val();
		var f_url = $('#f_url').val();
		var f_type = $('#f_type').val();
		var url = f_url;
		var type = '';
		if(f_type=='0'){
			type +='get';
		}else{
			type +='post';
		}
		var paGroup = $('.subApiTool').serialize();
		if(paGroup == ""){
			paGroup = '该接口无需传参';
		}

		var pas = '请求地址：'+url+'\n'+'请求参数：'+paGroup;
		$('#apiPa').html(pas);

		$('#apiBox').val('正在请求数据,请稍后~~');//原生结果
		$('#sdkBox').val('正在请求数据,请稍后~~');//翻译后的结果

		$.ajax({
			url:"{:U('ApiTool/getInfo')}",
			type:'post',
			data:{url:url,type:type,paGroup:paGroup},
			success:function(data){
				$('#apiBox').val(data.json);//原生结果
				if(data.status == 1){
					$('#sdkBox').val(JSON.stringify(JSON.parse(data.json), null, "    "));//翻译后的结果
				}else if(data.status == 0){
					$('#sdkBox').val('接口本身发生错误,请联系接口开发者......');//翻译后的结果
				}else if(data.status == 2){
					$('#sdkBox').val('接口服务器没有响应请求......');//翻译后的结果
				}

				$('#subTool').text('提交测试');
				$('#subTool').css({'background-color':'#428bca','border-color':'#428bca'});
			}
		});
	});

	function createPa(data,type){
	    var i = 0;
	    var html = '';
	        html+='<div class="form-group">';
	        html+='<label for="" class="col-sm-4 control-label">提交方式：</label>';
	        html+='<div class="col-sm-8">';
	        if(type==1){
	            html+='<label class="radio-inline">';
	            html+='<input type="radio" value="1" checked/> POST';
	            html+='</label>';
	        }else{
	            html+='<label class="radio-inline">';
	            html+='<input type="radio" value="0" checked/> GET';
	            html+='</label>';
	        }
	        html+='</div></div>';
	    for(i=0;i<data.length;i++){
	        html += '<div class="form-group create-pa">';
	        if(data[i].p_type=='0'){
	            html +='<label for="" class="col-xs-4 control-label">'+data[i].p_name+'：</label>';
	            html +='<div class="col-xs-6">'
                if(data[i].p_is_null =='0' && data[i].default_val){
                    //不可为空
                    html +='<input type="text" class="form-control must" value="' + data[i].default_val + '" id="'+data[i].p_name+'" name="'+data[i].p_name+'" placeholder="参数值">';
                }else{
                    //可为空
                    html +='<input type="text" class="form-control must" id="'+data[i].p_name+'" name="'+data[i].p_name+'" placeholder="参数值">';
                }

	            html +='</div>';
	            html +='<div class="col-xs-2 pa-tips">'
	            if(data[i].p_is_null=='0'){
	                html+='<em>*</em>';
	            }
	            html+='<a href="javascript:;" title="'+data[i].p_desc+'">说明</a>';
	            html +='</div>';
	        }else{
	            html +='<label for="" class="col-xs-4 control-label">';
	            html +=''+data[i].p_name+'：</label>';
	            html +='<div class="col-xs-6">'
	            html+='<input type="file" class="" id="'+data[i].p_name+'" name="'+data[i].p_name+'" />';
	            html +='</div>';
	            html +='<div class="col-xs-2">'
	            if(data[i].p_is_null=='0'){
	                html+='<em style="color:red">*</em>';
	            }
	            html+='<a href="javascript:;" title="'+data[i].p_desc+'">说明</a>';
	            html +='</div>';
	        }
	        html +='</div>';
	    }
	    $('#addParameters').html(html);
	}
</script>
<include file="Public:footer"/>